package com.qiantu.phone.ui.activity;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.hjq.http.listener.HttpCallback;
import com.hjq.widget.view.SwitchButton;
import com.qiantu.api.entity.SettingBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.ThemeUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.RefreshHomeDeviceSortSettingEvent;
import com.qiantu.phone.event.RefreshHomeWeatherSettingEvent;
import com.qiantu.phone.event.RefreshPushBannerSettingEvent;
import com.qiantu.phone.event.RefreshRecommendSceneSwitchEvent;
import com.qiantu.phone.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;

/**
 * time   : 2021/12/22
 */
public final class PagerCustomActivity extends AppActivity {

    private SwitchButton switchHomeOutdoorEnvironmentData;
    private SwitchButton switchCustomPushBanner;
    private SwitchButton switchSceneRecommend;
    private TextView tvCurrentStyle;
    private CheckBox btnRoomType;
    private CheckBox btnDeviceType;
    private SettingBean.SettingDTO.InterfaceCustomizationDTO data;
    //1 首页环境数据 2定制推送banner 3首页设备排布
    private int setType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pager_custom;
    }

    @Override
    protected void initView() {
        switchHomeOutdoorEnvironmentData = findViewById(R.id.switch_home_outdoor_environment_data);
        switchCustomPushBanner = findViewById(R.id.switch_custom_push_banner);
        switchSceneRecommend = findViewById(R.id.switch_scene_recommend);
        tvCurrentStyle = findViewById(R.id.tv_current_style);
        btnRoomType = findViewById(R.id.btn_room_type);
        btnDeviceType = findViewById(R.id.btn_device_type);
        switchSceneRecommend.setChecked(SPUtils.getInstance().getBoolean("switchSceneRecommend"));
        setOnClickListener(R.id.switch_home_outdoor_environment_data, R.id.switch_custom_push_banner, R.id.switch_scene_recommend, R.id.btn_select_style, R.id.btn_room_type, R.id.btn_device_type);
    }

    @Override
    protected void initData() {
        switch (ThemeUtil.themeId) {
            case ThemeUtil.THEME_DEFAULT:
                tvCurrentStyle.setText("科技蓝");
                break;
            case ThemeUtil.THEME_BLACK_GOLD:
                tvCurrentStyle.setText("黑金");
                break;
            case ThemeUtil.THEME_ORANGE:
                tvCurrentStyle.setText("橙色");
                break;
        }
        LLHttpManager.getInterfaceCustomizationSetting(this, new HttpCallback<HttpData<SettingBean.SettingDTO.InterfaceCustomizationDTO>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.common_loading));
            }

            @Override
            public void onSucceed(HttpData<SettingBean.SettingDTO.InterfaceCustomizationDTO> result) {
                data = result.getData();
                updateView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }

    private void updateView() {
        if (data == null) {
            return;
        }
        switchHomeOutdoorEnvironmentData.setChecked(data.isEnvironmentalData());
        switchCustomPushBanner.setChecked(data.isPushBanner());
        //首页设备排布类型 1：房间类型 2：设备类型
        switch (data.getTypesetting()) {
            case 1:
                btnRoomType.setChecked(true);
                btnDeviceType.setChecked(false);
                break;
            case 2:
                btnRoomType.setChecked(false);
                btnDeviceType.setChecked(true);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.switch_home_outdoor_environment_data:
                boolean isEnvironmentData = switchHomeOutdoorEnvironmentData.isChecked();
                switchHomeOutdoorEnvironmentData.setChecked(!isEnvironmentData);
                data.setEnvironmentalData(isEnvironmentData);
                setType = 1;
                setting();
                break;
            case R.id.switch_custom_push_banner:
                boolean pushBanner = switchCustomPushBanner.isChecked();
                switchCustomPushBanner.setChecked(!pushBanner);
                if (data!=null){
                    data.setPushBanner(pushBanner);
                }
                setType = 2;
                setting();
                break;
            case R.id.switch_scene_recommend:
                SPUtils.getInstance().put("switchSceneRecommend", switchSceneRecommend.isChecked());
                EventBus.getDefault().post(new RefreshRecommendSceneSwitchEvent());
                break;
            case R.id.btn_select_style:
                startActivity(SwitchThemeActivity.class);
                break;
            case R.id.btn_room_type:
                btnRoomType.setChecked(!btnRoomType.isChecked());
                data.setTypesetting(1);
                setType = 3;
                setting();
                break;
            case R.id.btn_device_type:
                btnDeviceType.setChecked(!btnDeviceType.isChecked());
                data.setTypesetting(2);
                setType = 3;
                setting();
                break;
        }
    }

    private void setting() {
        LLHttpManager.setInterfaceCustomizationSetting(this, data, new HttpCallback<HttpData<Void>>(this) {
            @Override
            public void onStart(Call call) {
                showDialog(getString(R.string.setting));
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                toast(R.string.set_success);
                if (AppApplication.getInstance().mSettingBean == null) {
                    AppApplication.getInstance().mSettingBean = new SettingBean();
                    SettingBean.SettingDTO settingDTO = new SettingBean.SettingDTO();
                    AppApplication.getInstance().mSettingBean.setSetting(settingDTO);
                }
                AppApplication.getInstance().mSettingBean.getSetting().setInterfaceCustomization(data);
                switch (setType) {
                    case 1:
                        EventBus.getDefault().post(new RefreshHomeWeatherSettingEvent());
                        break;
                    case 2:
                        EventBus.getDefault().post(new RefreshPushBannerSettingEvent());
                        break;
                    case 3:
                        EventBus.getDefault().post(new RefreshHomeDeviceSortSettingEvent());
                        break;
                }
                updateView();
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
            }

            @Override
            public void onEnd(Call call) {
                hideDialog();
            }
        });
    }
}